package org.sith.taskrunner.ejb;

import org.sith.taskrunner.api.TaskToolkitDescription;
import org.sith.taskrunner.api.toolkit.TaskToolkit;
import org.sith.taskrunner.ejb.exceptions.ToolkitNotFoundException;

import java.util.*;


public class TaskManagerHelper {
    private static TaskManagerHelper ourInstance = new TaskManagerHelper();

    private Map<String, TaskToolkit> toolkits;

    public static TaskManagerHelper getInstance() {
        return ourInstance;
    }

    private TaskManagerHelper() {
        toolkits = new HashMap<String, TaskToolkit>();
    }

    public void addToolkit(String key, TaskToolkit toolkit) {
        toolkits.put(key, toolkit);
    }

    public TaskToolkit getToolkit(String key) throws ToolkitNotFoundException {
        if (toolkits.containsKey(key)) {
            return toolkits.get(key);
        } else {
            throw new ToolkitNotFoundException("Toolkit for key " + key + " not found");

        }
    }


    public Set<TaskToolkitDescription<String>> getRegisteredToolkitsDescription() {
        Set<TaskToolkitDescription<String>> result = new HashSet<TaskToolkitDescription<String>>();

        Collection<TaskToolkit> toolkitBuilders = toolkits.values();
        for (TaskToolkit toolkitBuilder : toolkitBuilders) {
            result.add(toolkitBuilder.getToolkitDescription());
        }
        return result;
    }
}
